package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tgroup;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.TGroupDAO;
import com.ljkj.common.db.dao.ZjfGoodsAdDAO;
import com.ljkj.common.db.domain.ZjfGoodsAd;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.common.db.util.RandomCode;

public class GoodsADService {
	
	private ZjfGoodsAdDAO zjfGoodsAdDAO;
	private TLogService tLogService;

	/**
	 * 获取权限列表
	 * @param params 权限名称关键字
	 * @param currentPage 当前页
	 * @param pageSize 每页条数
	 * @param sortField 排序字段
	 * @param sortOrder 排序方式
	 * @return
	 */
	public String getGoodsADByProperties(String params,int currentPage,int pageSize,String sortField,String sortOrder){
		
		Class[] pojos = {ZjfGoodsAd.class};
		String[] propertyNames=null;
		Object[] values=null;
		String[] likePropertyNames = null;
		Object[] likevalues= null;
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfGoodsAdDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfGoodsAdDAO.setPageMaxCount(pageSize);
		List<ZjfGoodsAd> zjfGoodsAds = this.zjfGoodsAdDAO.findByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfGoodsAd zjfGoodsAd : zjfGoodsAds){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("adId",zjfGoodsAd.getAdId());
			entry_map.put("adMainImg",zjfGoodsAd.getAdMainImg());
			entry_map.put("adDetailsImg",zjfGoodsAd.getAdDetailsImg());
			entry_map.put("adName",zjfGoodsAd.getAdName());
			entry_map.put("adUrl",zjfGoodsAd.getAdUrl());
			entry_map.put("adSequence",zjfGoodsAd.getAdSequence());
			entry_map.put("createTime",zjfGoodsAd.getCreateTime());
			entry_map.put("createUser", zjfGoodsAd.getCreateUser());
			entry_map.put("updateTime", zjfGoodsAd.getUpdateTime());
			entry_map.put("updateUser", zjfGoodsAd.getUpdateUser());
			entry_map.put("disabledState", zjfGoodsAd.getDisabledState());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	
	
	
	/**
	 * 新增或更新广告
	 * @param zdgCoupon
	 * @return
	 */
	public String addOrEditGoodsADInfo(ZjfGoodsAd zjfGoodsAd,Tuser tuser){
		
		Tlog tlog = new Tlog();
		
		Map<String,String> result_map = new HashMap<String, String>();
		if(zjfGoodsAd.getAdId() == null){
			
			tlog.setOpType(1);
			tlog.setContent("新增广告："+ zjfGoodsAd.getAdName());
			
			zjfGoodsAd.setAdId(RandomCode.makeCode());
			zjfGoodsAd.setCreateTime(DateTime.getDateTime());
			zjfGoodsAd.setCreateUser(tuser.getLoginName());
			if(this.saveNewZjfGoodsAd(zjfGoodsAd)){
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				result_map.put("err_msg", "新增成功。");
				result_map.put("err_code", "0");
			}else{
				result_map.put("err_msg", "新增失败。");
				result_map.put("err_code", "1");
			}
			
		}else{
			try{
				
				if(zjfGoodsAd.getDisabledState()!=null && zjfGoodsAd.getDisabledState() == 1){
					tlog.setOpType(0);
					tlog.setContent("广告状态："+zjfGoodsAd.getAdName()+" 变为不可用");
				}else{
					tlog.setOpType(1);
					tlog.setContent("修改广告："+zjfGoodsAd.getAdName());
				}
				
				zjfGoodsAd.setUpdateTime(DateTime.getDateTime());
				zjfGoodsAd.setUpdateUser(tuser.getLoginName());
				this.zjfGoodsAdDAO.update(zjfGoodsAd);
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				
				result_map.put("err_msg", "更新成功。");
				result_map.put("err_code", "0");
			}catch(Exception ex){
				result_map.put("err_msg", "更新失败。");
				result_map.put("err_code", "1");
			}
		}
		
		return JSON.toJSONString(result_map);
	}
	
	public boolean saveNewZjfGoodsAd(ZjfGoodsAd zjfGoodsAd){
		try{
			this.zjfGoodsAdDAO.save(zjfGoodsAd);
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	
	public ZjfGoodsAd getGoodsADById(String goods_ad_ID){
		return this.zjfGoodsAdDAO.findById(goods_ad_ID);
	}

	
	
	//注入DAO
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}

	public void setZjfGoodsAdDAO(ZjfGoodsAdDAO zjfGoodsAdDAO) {
		this.zjfGoodsAdDAO = zjfGoodsAdDAO;
	}

}
